<template>
    <el-popover
            placement="left"
            width="250"
            trigger="manual"
            v-model="popVisible">
        <div style="display: flex;justify-content: space-between ">
            <span>请选择部门</span>
            <i class="el-icon-close" style="margin-top: 2px" @click="popVisible=false"></i>
        </div>
        <el-tree
                :data="allDepartments"
                :props="defaultProps"
                default-expand-all
                @node-click="handleNodeClick"></el-tree>
        <div slot="reference" class="depdiv" @click="showDepartmentView">{{departmentName}}</div>
    </el-popover>
</template>

<script>
    export default {
        name: "depChoose",
        props:{
            departmentId:Number,
            departmentName:String
        },
        data(){
            return{
                popVisible:false,
                allDepartments:null,
                defaultProps: {
                    children: 'children',
                    label: 'name'
                }
            }
        },
        created() {
            this.getDepData();
        },
        methods:{
            //点击树形结构上任意节点即关闭弹出框,回调参数:共三个参数，依次为：传递给 data 属性的数组中该节点所对应的对象、节点对应的 Node、节点组件本身。
            handleNodeClick(data) {
                this.$emit('isClose',data);
                this.popVisible = !this.popVisible;
            },
            //展示部门结构树
            showDepartmentView(){
                this.popVisible = !this.popVisible;
            },
            getDepData(){
                if (!window.sessionStorage.getItem("allDepartments")) {
                    this.getRequest("/employee/basic/getalldepartments").then(resp => {
                        if (resp) {
                            this.allDepartments = resp;
                            window.sessionStorage.setItem("allDepartments", JSON.stringify(resp));
                        }
                    })
                } else {
                    this.allDepartments = JSON.parse(window.sessionStorage.getItem("allDepartments"));
                }
            }
        }
    }
</script>

<style scoped>

</style>
